package com.example.neusoftfood_828lhl.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.neusoftfood_828lhl.R;
import com.example.neusoftfood_828lhl.activity.FoodListActivity;
import com.example.neusoftfood_828lhl.bean.ShopBean;
import com.example.neusoftfood_828lhl.common.Constants;
import com.squareup.picasso.Picasso;


import java.util.List;

public class ShopAdapter extends RecyclerViewAdapter<ShopAdapter.ShopViewHolder> implements  View.OnClickListener{
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener = null;
    public ShopAdapter(Context mContext, List mDataList,int layoutResource) {
        super(mContext,mDataList,layoutResource);
    }
    @Override
    public ShopViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        v.setOnClickListener(this);
        return new ShopViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ShopAdapter.ShopViewHolder shopViewHolder, int position) {
        final ShopBean entity = (ShopBean)data.get(position);
        if (null == entity)
            return;
        shopViewHolder.res_name.setText(entity.getShopname());
        shopViewHolder.res_address.setText(entity.getAddress());
        shopViewHolder.res_bar.setRating(entity.getLevel());
        Picasso.with(context).load(Constants.URL+entity.getPic()).into(shopViewHolder.image);
        shopViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, FoodListActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                context.startActivity(intent);
            }
        });
        shopViewHolder.itemView.setTag(entity);
    }


    @Override
    public void onClick(View v) {
        if(onRecyclerViewItemClickListener!=null){
            onRecyclerViewItemClickListener.onItemClick(v,v.getTag());
        }
    }

    public static class ShopViewHolder extends RecyclerView.ViewHolder {
        RatingBar res_bar;
        TextView res_name, res_address;
        ImageView image;
        public ShopViewHolder(View view) {
            super(view);
            res_name = (TextView) view.findViewById(R.id.res_name);
            res_bar = (RatingBar) view.findViewById(R.id.ratingBar1);
            image = (ImageView) view.findViewById(R.id.res_image);
            res_address = (TextView) view.findViewById(R.id.res_address);
        }
    }
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , Object data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener){
        this.onRecyclerViewItemClickListener = listener;
    }
}
